package com.clstu.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionIterator {
    public static void main(String[] args) {
        Collection list=new ArrayList();
        list.add(new Book("三国演义","罗贯中",123));
        list.add(new Book("西游记","吴承恩",123));
        list.add(new Book("水浒传","施耐庵",123));
        list.add(new Book("红楼梦","曹雪芹",123));
        list.add("jackchen");
        Iterator iterator=list.iterator();
        while (iterator.hasNext()) {//快速调出的方法itit
            Object next =  iterator.next();
            System.out.println(next);//根据动态绑定机制调用运行类型的toString方法来打印出对象。
        }
        //ystem.out.println(iterator.next());//NoSuchElementException异常，因为此时迭代器已经指向了最后一个，再后面没有了
        //若要重新迭代，则重置迭代器
        iterator=list.iterator();//重置了迭代器,又可以迭代了
        while (iterator.hasNext()) {//hasNext()用于判断是否还有next，若没有则返回false
            Object next =  iterator.next();
            System.out.println(next);
        }
    }
}
class Book{
    private String name;
    private String author;
    private double price;

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price
                ;
    }
}